<?php

	$dir = pathinfo($_SERVER['SCRIPT_FILENAME']); 

	if (ini_set("include_path",ini_get("include_path").((strtoupper(PHP_OS) == "LINUX") ? ":" : ";").$dir['dirname']) === false) {
		echo "ERROR: Definicion en el include_path";
		exit;
	}

	include('../adodb/adodb.inc.php');
	include_once('../adodb/adodb-pager.inc.php');
    include_once('../conf/dbconn_param.php');
	
	
	$db = ADONewConnection($dbdriver);
	$db->Connect($server, $user, $password, $database);
	$db->SetFetchMode(ADODB_FETCH_ASSOC);

    $db->debug = false;

	$sql = 'select *
		   from finca';
    $rs = $db->Execute($sql);
    $fincas = $rs->GetRows();

	$sql = 'select *
		   from producto';
    $rs = $db->Execute($sql);
    $productos = $rs->GetRows();
			
	$sql = 'select *
		   from variedad';
    $rs = $db->Execute($sql);
    $datos = $rs->GetRows();
	
	echo '{"iTotalRecords":'.$rs->RecordCount().',
         "iTotalDisplayRecords":50,
         "aaData":['; 
	
	
	/*foreach ($datos as $i => $value) 
			foreach ($value as $j => $data)
				$datos[$i][$j] = utf8_encode($data);  
	echo js_object($datos);
			*/

	$i = 0;
	foreach($datos as $data_) {	
	     if ($i++) echo ',';
		 
		 $activo = ($data_['activo'] == '1') ? 'Si' : 'No';		
	
		$producto="";
		foreach($productos as $productos_) {	
	     
		 
		 	$encontrado = ($productos_['producto_id'] == $data_['producto_id']) ? '1' : '0';		
		 	if ($encontrado=='1'){ $producto=$productos_['nombre'];  }
       	 
    	}
		 
         echo '[',$data_['variedad_id'],',
           "',utf8_encode($data_['nombre']),'",
           "',utf8_encode($data_['codigo']),'",	              
		   "',$producto,'",		
           "',$activo,'",
           "',utf8_encode($data_['finca_id']),'",           
		   "',utf8_encode($data_['longitud']),'",
		   "',utf8_encode($data_['color']),'",
		   "',utf8_encode($data_['descripcion']),'",
		   "',utf8_encode($data_['foto']),'",
		   "',utf8_encode($data_['tallos_malla']),'",	
	   	"',utf8_encode($data_['ciclo']),'"]';        	
    }
	
	
	
	echo ']}'; 
	 $db->Close();
?>	